package fr.synchrone.starsudoku;


public class GamePart {
 	private int[][] result = new int[9][9];
 	private int[][] game = new int[9][9];
 	private int[] num = {1,2,3,4,5,6,7,8,9};
 	private int level;
    
	public GamePart(int level){
		this.level = 81 - level;
		init();
	}
	
	public boolean validateCase(int x, int y, int value){
    	return value == result[x][y];
    }
	
	public int[][] getGame() {
		return game;
	}
	
	private void init(){
		initCases();
		for(int i = 0;i < level;i++){
	       int ranx = (int)(Math.random() * 9);
	       int rany = (int)(Math.random() * 9);
	       if(game[ranx][rany] != result[ranx][rany]){
	    	   game[ranx][rany]=result[ranx][rany];
	       }
	       else{
	    	   i--;
	       }
		}
	}
	
	private void initCases(){
		for(int i = 0;i < 9;i++){
            int time = 0;
            for(int j = 0;j < 9;j++){
            	result[i][j] = generateNum(i,j,time);    
                  if(result[i][j] == 0){
                          if(j > 0){
                                      j-=2;
                                      continue;
                         }else{
                                     i--;
                                     j = 8;
                                     continue;
                         }
                   } 
                   if(isCorret(i,j)){
                         time = 0;
                   }else{ 
                        time++;
                        j--;
                  }    
            	}             
            }
 
//		for(int i = 0;i < 9;i++){
//              for(int j = 0;j < 9;j++){
//                         System.out.print(n[i][j] + "  ");
//              }
//              System.out.println();
//		}
	}
	private boolean isCorret(int row,int col){
        return (checkRow(row) & checkLine(col) & checkNine(row,col));
    }
    private boolean checkNine(int row,int col){
		int j = row / 3 * 3;
		int k = col /3 * 3;
		for(int i = 0;i < 8;i++){
			if(result[j + i/3][k + i % 3] == 0){
		               continue;
		   }
		  for(int m = i+ 1;m < 9;m++){
		              if(result[j + i/3][k + i % 3] == result[j + m/3][k + m % 3]){
		                           return false;
		              }
		   }
		}
		return true;
	}
	private boolean checkRow(int row){  
        for(int j = 0;j < 8;j++){
               if(result[row][j] == 0){
                        continue;
               }
              for(int k =j + 1;k< 9;k++){
                       if(result[row][j] == result[row][k]){
                                return false;
                       }
              }
         }
        return true;
	}
	private boolean checkLine(int col){
      for(int j = 0;j < 8;j++){
             if(result[j][col] == 0){
                     continue;
             }
             for(int k =j + 1;k< 9;k++){
                     if(result[j][col] == result[k][col]){
                              return false;
                     }
             }
      }
      return true;
    }
	private int generateNum(int row,int col,int time){
       if(time == 0){
             for(int i = 0;i < 9;i++){
                     num[i] = i + 1;
             }
       }
       if(time == 9){
                return 0;
       }  
       int ranNum = (int)(Math.random() * (9 - time));
       int temp = num[8 - ranNum];
       num[8 - ranNum] = num[ranNum];
       num[ranNum] = temp;
       return num[8 - ranNum];  
	}
}
